package com.atguigu.tingshu.user.receiver;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * ClassName: UserReceiver
 * Package: com.atguigu.tingshu.user.receiver
 * Description:
 *
 * @Author:
 * @Create: 2025/8/6 - 16:30
 * @Version: v1.0
 */
@Component
@Slf4j
public class UserReceiver {

    @Resource
    private UserInfoService userInfoService;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD)
    public void updateUserPaidRecord(ConsumerRecord<String, String> record) {
        String value = record.value();
        if (StringUtils.hasText(value)) {
            UserPaidRecordVo userPaidRecordVo = JSON.parseObject(value, UserPaidRecordVo.class);
            this.userInfoService.updateUserPaid(userPaidRecordVo);
        }
    }

}
